//
//  YFHomeRollView.m
//  QuanJieShopProject
//
//  Created by apple on 2018/5/14.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "YFHomeRollView.h"
#import "HWWeakTimer.h"

@interface YFHomeRollContainerView()
@property (nonatomic, strong) UILabel *titleLabel;
@end

@implementation YFHomeRollContainerView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self makeView:frame];
    }
    return self;
}

- (void)makeView:(CGRect)frame {
    
    CGFloat imgViewWH = self.mj_h * 0.8;
    CGFloat imgViewY = (self.mj_h - imgViewWH)/2;
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, imgViewY, imgViewWH, imgViewWH)];
    imgView.image = [UIImage imageNamed:@"gonggao"];
    [self addSubview:imgView];
    
    CGFloat labelX = CGRectGetMaxX(imgView.frame) + 5;
    CGFloat labelW = self.mj_w - labelX;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(labelX, 0, labelW, self.mj_h)];
    label.text = @"分享注册即可获得5元红包!";
    label.font = [UIFont systemFontOfSize:12];
    if (isLargeriPhone6) {
        label.font = [UIFont systemFontOfSize:13];
    }
    label.textColor = YFColor_Text;
    [self addSubview:label];
    self.titleLabel = label;
    
}

@end

@interface YFHomeRollView()
@property (nonatomic, strong) YFHomeRollContainerView *container1;
@property (nonatomic, strong) YFHomeRollContainerView *container2;
@property (nonatomic, strong) YFHomeRollContainerView *container3;

@property (nonatomic, weak) NSTimer *timer;



@end

@implementation YFHomeRollView

- (void)setDataArr:(NSArray *)dataArr {
    _dataArr = dataArr;
    
    for (int i=0; i<dataArr.count; i++) {
        HomeLoopModel *model = dataArr[i];
        if (i==0) {
            self.container1.titleLabel.text = model.title;
        }else if (i==1) {
            self.container2.titleLabel.text = model.title;
        }else if (i==2) {
            self.container3.titleLabel.text = model.title;
        }
    }

}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self makeView];
        
//        UIButton *redButton2 = [[UIButton alloc] init];
//        redButton2.frame = CGRectMake(0, 0, frame.size.width/2, frame.size.height);
//        [redButton2 setTitle:@"开始" forState:UIControlStateNormal];
//        [redButton2 addTarget:self action:@selector(redButton2Click) forControlEvents:UIControlEventTouchUpInside];
//        redButton2.backgroundColor = [UIColor redColor];
//        [self addSubview:redButton2];
//
//        UIButton *redButton = [[UIButton alloc] init];
//        redButton.frame = CGRectMake(frame.size.width/2, 0, frame.size.width/2, frame.size.height);
//        [redButton setTitle:@"暂停" forState:UIControlStateNormal];
//        [redButton addTarget:self action:@selector(redButtonClick) forControlEvents:UIControlEventTouchUpInside];
//        redButton.backgroundColor = [UIColor redColor];
//        [self addSubview:redButton];
    }
    return self;
}




- (void)redButton2Click {
    
    [self startTimer];
}

- (void)redButtonClick {
    
    [self stopTimer];
}

- (void)timerFired:(NSTimer *)timer {
    NSLog(@"调用了定时器=====");
    [UIView animateWithDuration:0.25
                    animations:^{
                      self.container1.mj_y -= self.mj_h/2;
                      self.container2.mj_y -= self.mj_h/2;
                      self.container3.mj_y -= self.mj_h/2;

                    } completion:^(BOOL finished) {
                      if (self.container1.mj_y <= -self.mj_h/2) {
                          self.container1.mj_y = self.mj_h;
                      }
                      if (self.container2.mj_y <= -self.mj_h/2) {
                          self.container2.mj_y = self.mj_h;
                      }
                      if (self.container3.mj_y <= -self.mj_h/2) {
                          self.container3.mj_y = self.mj_h;
                      }
                    }];
}

- (void)makeView {

    self.container1.titleLabel.text = @"这是第一个";
    self.container2.titleLabel.text = @"这是第2个";
    self.container3.titleLabel.text = @"这是第3个";
    
    self.clipsToBounds = YES;
    
    // 测试的时候2秒, 正式的时候5s
    kWeakSelf
//    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:weakSelf selector:@selector(timerFired:) userInfo:nil repeats:YES];
//    _timer = timer;
//    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    
    [self startTimer];
}

- (YFHomeRollContainerView *)container1 { // top
    if (!_container1) {
        _container1 = [[YFHomeRollContainerView alloc] initWithFrame:CGRectMake(0, 0, self.mj_w, self.mj_h/2)];
        [self addSubview:_container1];
    }
    return _container1;
}

- (UIView *)container2 {  // bottom
    if (!_container2) {
        _container2 = [[YFHomeRollContainerView alloc] initWithFrame:CGRectMake(0, self.mj_h/2, self.mj_w, self.mj_h/2)];
        [self addSubview:_container2];
    }
    return _container2;
}

- (UIView *)container3 {  // hidden
    if (!_container3) {
        _container3 = [[YFHomeRollContainerView alloc] initWithFrame:CGRectMake(0, self.mj_h, self.mj_w, self.mj_h/2)];
        [self addSubview:_container3];

    }
    return _container3;
}

- (void)startTimer {
    NSLog(@"startTimer===");
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
    _timer = timer;
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    
//    _timer = [HWWeakTimer scheduledTimerWithTimeInterval:1.0f block:^(id userInfo) {
//        NSLog(@"%@", userInfo);
//    } userInfo:@"Fire" repeats:YES];
//    [_timer fire];
}

- (void)stopTimer {
    NSLog(@"stopTimer===");
        
    // 调用这个没有用
    [self.timer invalidate];
    self.timer = nil;

//    _timer = [HWWeakTimer scheduledTimerWithTimeInterval:1.0f block:^(id userInfo) {
//        NSLog(@"%@", userInfo);
//    } userInfo:@"Fire" repeats:NO];
//    [_timer fire];

}


//*/
@end
